package com.itheima.ydd.controller;


import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.dto.RoleDto;
import com.itheima.ydd.pojo.SysRole;
import com.itheima.ydd.service.SysRoleMenuService;
import com.itheima.ydd.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("sys_role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 根据角色ID修改
     * @param roleDto
     * @return
     */
    @PostMapping("update")
    public ResponseEntity<R<String>> update(@RequestBody RoleDto roleDto){
        return sysRoleService.updateSysRole(roleDto);
    }


    /**
     * 添加角色
     * @param roleDto
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<R<String>> add(@RequestBody RoleDto roleDto){
        return sysRoleService.addSysRole(roleDto);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResponseEntity<R<String>> del(@RequestParam String id){
        return sysRoleService.delSysRole(id);
    }

    /**
     * 分页
     * @param page 页码
     * @param limit 每页数量
     * @param name  角色名称,支持模糊查询
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<R<PageInfo<SysRole>>> list(@RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
                                                     @RequestParam(value = "limit",required = false,defaultValue = "10") Integer limit,
                                                     @RequestParam(value = "name")String name){
        return sysRoleService.findPageOrName(page,limit,name);

    }

    /**
     * id查询,数据回显
     * @param id 角色id
     * @return
     */
    @GetMapping("rowInfo")
    public ResponseEntity<R> rowInfo(String id){
        return sysRoleService.findById(id);



    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("allData")
    public ResponseEntity<R<List<SysRole>>> find(){
        return sysRoleService.findAllData();
    }
}
